<script setup>
import {useRouter} from "vue-router";
import TeamCardList from "../components/TeamCardList.vue";
import myAxios from "../plugins/myAxios.ts";
import {onMounted, ref} from "vue";
import {showFailToast, showSuccessToast} from "vant";

const router = useRouter();

// 跳转到加入队伍页
const onJoinTeam = () => {
  router.push({
    path: '/team/add'
  })
}

const teamList = ref([]);
const searchText = ref('');
const active = ref('public');

const onSearch = (val) => {
  listTeam(val);
};

/**
 * 切换查询状态
 * @param name
 */
const onTabChange = (name) => {
  // 查公开
  if (name === 'public') {
    listTeam(searchText.value, 0);
  } else {
    // 查加密
    listTeam(searchText.value, 2);
  }
}

/**
 * 搜索队伍
 * @param val
 * @param status
 * @returns {Promise<void>}
 */
const listTeam = async (val = '', status = 0) => {
  const res = await myAxios.get("/team/list", {
    params: {
      searchText: val,
      pageNum: 1,
      status,
    },
  });
  if (res?.code === 0) {
    teamList.value = res.data;
    showSuccessToast('队伍加载成功');
  } else {
    showFailToast('队伍加载失败，请刷新重试');
  }
}

onMounted( () => {
  listTeam();
})
</script>

<template>
  <div id="teamPage">
    <van-search v-model="searchText" placeholder="搜索队伍" @search="onSearch" />
    <van-tabs v-model:active="active" @change="onTabChange">
      <van-tab title="公开" name="public" />
      <van-tab title="加密" name="private" />
    </van-tabs>
    <div style="margin-bottom: 16px" />
    <van-button class="add-team" type="primary" icon="plus" round @click="onJoinTeam" />
    <team-card-list :team-list="teamList" />
    <van-empty v-if="teamList?.length < 1" description="数据为空"/>
  </div>
</template>

<style scoped>
.add-team {
  width: 60px;
  height: 60px;
  position: fixed;
  bottom: 90px;
  right: 40px;
}
</style>